import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map } from "rxjs/operators";
import { Subject } from 'rxjs';
import { Observable } from 'rxjs';
@Injectable()
export class CustomService {
    private routeMsgPushSource = new Subject<any>();
    routeMsgPush$ = this.routeMsgPushSource.asObservable();
    constructor(private http: HttpClient) { }
    /** 加载自定义业务类别 */
    loadCustomBiz() {
        return this.http.get('/custom/api/config/loadcustombiz').pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 加载自定义业务信息 */
    loadCustomBizItem(itemId: number){
        return this.http.get('/custom/api/config/loadcustombizitem?itemId=' + itemId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 更新业务信息 */
    updateCustomBiz(item : any){
        return this.http.post('/custom/api/config/updatecustombizitem', item).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 加载自定义业务类型 */
    loadBizItemTree() {
        return this.http.get('/custom/api/config/listcustombiztree').pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 创建自定义业务 */
    createCustomBiz(biz: any) {
        return this.http.post('/custom/api/config/addcustombiz', biz).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 删除自定义业务 */
    deleteCustomItem(itemId: any){
        return this.http.get('/custom/api/config/deletecustombizitem?itemId='+itemId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 加载数据库业务表 */
    loadCustomTables() {
        return this.http.get('/custom/api/config/loadcustomtables').pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 加载已保存的数据信息 */
    loadCustomTableFields(itemId: number, tableName: string = "") {
        return this.http.get('/custom/api/config/loadcustomtablefields?itemId=' + itemId + '&tableName=' + tableName).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 保存自定义业务字段信息 */
    saveCustomTableFields(itemId: number, table: string, fields: any[]) {
        return this.http.post('/custom/api/config/savecustomfields?itemId=' + itemId + '&tableName=' + table , fields).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 保存生成输入表单save */
    saveCustomTableForAssemblies( formId: number, fields: any[]) {
        return this.http.post('/custom/api/config/saveCustomTableForAssemblies?formId=' + formId, fields).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 加载配置项 */
    loadConfigDetail(bizId: number, taskListGroupId: number = 0) {
        return this.http.get('/custom/api/config/load?bizId=' + bizId + '&taskListGroupId='+taskListGroupId).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 加载权限 */
    loadTaskListGroup(bizId: number){
        return this.http.get('/custom/api/config/loadtasklistgroup?bizId=' + bizId).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 主表单保存 */
    saveUpdate(formValue: any) {
        return this.http.post('/custom/api/biz/saveupdate', formValue).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 加载字段信息 */
    loadFieldNames(itemId: number) {
        return this.http.get('/custom/api/config/loadfieldname?itemId=' + itemId).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 加载组件类型 */
    loadFormAssemblyDef() {
        return this.http.get('/custom/api/config/loadFormAssemblyDef').pipe(
            map((res: Response) => res['data'])
        );
    }

    /**父子组件通讯 */
    announceMission(data: any) {
        this.routeMsgPushSource.next(data);
    }
}
